package com.book.bookmanager.service;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.book.bookmanager.mapper.bookMapper;
import com.book.bookmanager.pojo.Book;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional(rollbackFor = Exception.class)
public class bookServiceImpl implements bookService {
	@Autowired
	private bookMapper bd;
	@Autowired
	private RedisTemplate redisTemplate;

	@Override
	public Map<String, Object> queryAll(String token) {
		//从redis获取信息
		Object obj = redisTemplate.opsForValue().get(token);
		if (obj != null) {
		List<Book> list = bd.selectAll();
		Map<String, Object> data = new HashMap<>();
		data.put("books",list);
		return data;
	}
	return null;
	}

	@Override
	public void removeById(Integer id) {
		try {
			bd.deleteById(id);
		} catch (Exception e) {
			throw new RuntimeException("删除错误");
		}
	}

	@Override
	public void addBook(Book book) {
		try {
			bd.insertBook(book);
		} catch (Exception e) {
			throw new RuntimeException("插入错误");
		}
	}

	@Override
	public Book queryById(Integer id) {
		return bd.selectById(id);
	}

	@Override
	public void changeBook(Book book) {
		try {
			bd.updateBook(book);
		} catch (Exception e) {
			throw new RuntimeException("插入错误");
		}
	}
	@Override
	public void addPhoto(Book book){
		try {
			bd.addPhoto(book);
		}catch (Exception e){
			throw new RuntimeException("图片错误");
		}
	}

    @Override
    public IPage<Book> selectPage(Page<Book> page) {

		return bd.selectPage(page);
    }
}
